<?php
class PaymentHandler{

 private $regno;
 private $subgr;
 private $con;
 
 
public function setRegno($reg){
	
	$this->regno=$reg;
	}

public function setSubgr($sub){
	
	$this->subgr=$sub;
	}

public function setConnect($conn){
	 
	 return $this->con=$conn;
	 }




public function paymentAdd($rec){

$time_now=mktime(date('H')+5,date('i')+30,date('s'),date('m'),date('d'),date('Y'));

$year=date('Y',$time_now);
$paid=date('Y-m-d H:i:s',$time_now);
$mon=date('m',$time_now);

$result1=mysql_query("select * from student where reg_no='$this->regno' and year(j_date)='$year'",$this->con) or die(mysql_error());

$result2=mysql_query("select c_fee from subject_code where sub_gr='$this->subgr'",$this->con) or die(mysql_error());
$result3=mysql_query("select * from payment where reg_no='$this->regno' and sub_gr='$this->subgr' and month(paid_date)='$mon'  ") or die(mysql_error());

$count1=mysql_num_rows($result1);
$count2=mysql_num_rows($result3);



if(!($count1==0)){//checking Registration no is valid in this year 

if($count2==0){//checking student paid for that subjetc early

while($row=mysql_fetch_array($result2)){
$fee=$row['c_fee'];

}

$sql="insert into payment (reg_no,sub_gr,paid_date,p_fee,recieved_by) values('$this->regno','$this->subgr','$paid',$fee,'$rec')";

mysql_query($sql,$this->con) or die(mysql_error());

$pid=mysql_insert_id();

$msg=$pid;
return $msg;

}else{

$msg=" Already Paid";
return $msg;

}

} else{

$msg="Invalid Registration No";
return $msg;

}


}




public function getPageSearch($startrow,$limit){//getting data to initials searchin page 


/*$sql="select payment.payment_id,payment.reg_no,subject_code.grade,subject_code.subject,subject_code.fee,month(payment.paid_date) as date from payment inner join subject_code on payment.sub_gr=subject_code.sub_gr order by payment.payment_id desc  ";
 */
 /*$sql="select payment.payment_id,payment.reg_no,subject_code.grade,subject_code.subject,subject_code.fee,month(payment.paid_date) as date from payment,subject_code where payment.sub_gr=subject_code.sub_gr order by payment.payment_id desc Limit $startrow,15";*/

$sql="select payment.payment_id,payment.reg_no,subject_code.grade,subject_code.subject,payment.p_fee,month(payment.paid_date) as date from payment,subject_code where payment.sub_gr=subject_code.sub_gr order by payment.payment_id desc Limit $startrow,$limit";


$result=mysql_query($sql,$this->con)or die(mysql_error());
return $result;

 }
 
 
 
 
 
 public function getSearch($startrow,$psearch,$pid,$mon,$limit){
 
 if($pid=='payment'){

$sql="select payment.payment_id,payment.reg_no,subject_code.grade,subject_code.subject,payment.p_fee,month(payment.paid_date) as date from payment,subject_code where payment.sub_gr=subject_code.sub_gr and payment.payment_id='$psearch' order by payment.payment_id desc Limit $startrow,$limit";
 
  }else{
 
 $sql="select payment.payment_id,payment.reg_no,subject_code.grade,subject_code.subject,payment.p_fee,month(payment.paid_date) as date from payment,subject_code where payment.sub_gr=subject_code.sub_gr and payment.reg_no='$psearch' and 
 month(payment.paid_date)='$mon' order by payment.payment_id desc Limit $startrow,$limit";
 }
 $result=mysql_query($sql,$this->con) or die(mysql_error());
 return $result;
 
 }
 



public function paymentUpdate($pid){

$time_now=mktime(date('H')+5,date('i')+30,date('s'),date('m'),date('d'),date('Y'));

$year=date('Y',$time_now);

$result1=mysql_query("select * from student where reg_no='$this->regno' and year(j_date)='$year'",$this->con) or die(mysql_error());
$result2=mysql_query("select c_fee from subject_code where sub_gr='$this->subgr'",$this->con) or die(mysql_error());
$result3=mysql_query("select * from payment where reg_no='$this->regno' and sub_gr='$this->subgr'") or die(mysql_error());

$count1=mysql_num_rows($result1);
$count2=mysql_num_rows($result3);

$result=mysql_query("select c_fee from subject_code where sub_gr='$this->subgr'",$this->con);

if(!($count1==0)){//checking Registration no is valid in this year 

if($count2==0){//checking student paid for that subjetc early

while($row=mysql_fetch_array($result)){

$fee=$row['c_fee'];

        }

$sql="update payment set reg_no='$this->regno',sub_gr='$this->subgr',p_fee='$fee' where payment_id='$pid'";
mysql_query($sql,$this->con) or die(mysql_error());

$msg="Successfully Updated";

return $msg;
}else{

$msg=" Already Paid";
return $msg;

}

} else{

$msg="Invalid Registration No";
return $msg;

}

}




public function getReport($pid){

$sql="select payment.payment_id,payment.reg_no,subject_code.grade,subject_code.subject,payment.p_fee,date(payment.paid_date) as date,recieved_by from payment,subject_code where payment.sub_gr=subject_code.sub_gr and payment.payment_id='$pid'";

$result=mysql_query($sql,$this->con) or die(mysql_error());

return $result;

}




public function getPaginationRow($psearch,$pid,$mon){
 
if($pid=='payment'){

$sql="select payment.payment_id,payment.reg_no,subject_code.grade,subject_code.subject,payment.p_fee,month(payment.paid_date) as date from payment,subject_code where payment.sub_gr=subject_code.sub_gr and payment.payment_id='$psearch'";
 
 }else{
 
 $sql="select payment.payment_id,payment.reg_no,subject_code.grade,subject_code.subject,payment.p_fee,month(payment.paid_date) as date from payment,subject_code where payment.sub_gr=subject_code.sub_gr and payment.reg_no='$psearch' and 
 month(payment.paid_date)='$mon' ";
 
 }
 $result=mysql_query($sql,$this->con) or die(mysql_error());
 return $result;
 
 }
} 
?>